package com.zd.springcloud.config.callback;


import com.zd.springcloud.config.OrderClient;
import com.zd.springcloud.domain.Order;
import io.seata.core.context.RootContext;
import io.seata.tm.api.GlobalTransactionContext;
import org.springframework.stereotype.Component;

@Component
public class OrderClientCallbackImpl implements OrderClient {

    //降级的方法
    @Override
    public String addOrder(Order order) {

        System.out.println("===========熔断降级方法执行===============");

        if(RootContext.inGlobalTransaction()){
            System.out.println("===========当前调用在全局事务中，正在手动回滚===============");
            try{
                GlobalTransactionContext.reload(RootContext.getXID()).rollback();
            }catch (Exception e){
                System.out.println("===========当前调用在全局事务中，手动回滚失败===============");
                throw new RuntimeException();
            }
            System.out.println("===========当前调用在全局事务中，手动回滚成功===============");

        }

        return "abc";
    }
}
